# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.pro>

_realname=s2geometry
_pyname=pywraps2
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}" "${MINGW_PACKAGE_PREFIX}-python-${_pyname}")
pkgver=0.10.0
pkgrel=4
pkgdesc="A library for manipulating geometric shapes (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://s2geometry.io/'
license=('spdx:Apache-2.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-openssl"
             "${MINGW_PACKAGE_PREFIX}-abseil-cpp"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-swig")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/google/s2geometry/archive/v${pkgver}.tar.gz"
        "0001-s2geometry-0.10.0-fix-python-install.patch"
        "0002-s2geometry-0.10.0-use-math-defines.patch"
        "0003-s2geometry-0.10.0-python-point-named-component.patch")
sha256sums=('1c17b04f1ea20ed09a67a83151ddd5d8529716f509dde49a8190618d70532a3d'
            'a25e08cdf5aa1559c6d91f8d7c1302a71c2ed70f4ca28d5ae5ee7818bcd881ab'
            '7e30f275ab8ae9969decb93ac744052cdfb2c86d1b18de29e053edf79f2cb20d'
            '90577b86d7b9a77c9a2172664065410c83eb5251166f1918de326a435745d7e0')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  patch -Np1 -i "${srcdir}"/0001-s2geometry-0.10.0-fix-python-install.patch
  patch -Np1 -i "${srcdir}"/0002-s2geometry-0.10.0-use-math-defines.patch
  patch -Np1 -i "${srcdir}"/0003-s2geometry-0.10.0-python-point-named-component.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_{SHARED,STATIC}_LIBS=ON \
      -DWITH_PYTHON=ON \
      -DPython3_EXECUTABLE="${MINGW_PREFIX}/bin/python.exe" \
      ../${_realname}-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package_s2geometry() {
  depends=("${MINGW_PACKAGE_PREFIX}-openssl"
           "${MINGW_PACKAGE_PREFIX}-abseil-cpp")
  optdepends=("${MINGW_PACKAGE_PREFIX}-python-${_pyname}=${pkgver}-${pkgrel}: Python bindings")

  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}

package_python-pywraps2() {
  pkgdesc="Python bindings for s2geometry (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}=${pkgver}-${pkgrel}"
           "${MINGW_PACKAGE_PREFIX}-python")

  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install . --component python

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_pyname}/LICENSE"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :
# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
